'''
numpy 是数学、矩阵操作
pandas是基于numpy构建的，类似于一张带有行列索引的表
首先需要了解他主要两个数据结构：Series和DataFrame
'''

import numpy as np
import pandas as pd

# 索引默认0到N-1
s = pd.Series([1, 3, 6, np.nan, 44, 1])

print(s)
"""
0     1.0
1     3.0
2     6.0
3     NaN
4    44.0
5     1.0
dtype: float64
"""

# DataFrame 行索引也有列索引
dates = pd.date_range('20160101', periods=6)
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=['a', 'b', 'c', 'd'])

print(df)
"""
                   a         b         c         d
2016-01-01 -0.253065 -2.071051 -0.640515  0.613663
2016-01-02 -1.147178  1.532470  0.989255 -0.499761
2016-01-03  1.221656 -2.390171  1.862914  0.778070
2016-01-04  1.473877 -0.046419  0.610046  0.204672
2016-01-05 -1.584752 -0.700592  1.487264 -1.778293
2016-01-06  0.633675 -1.414157 -0.277066 -0.442545
"""
# 取列
print(df['b'])

"""
2016-01-01   -2.071051
2016-01-02    1.532470
2016-01-03   -2.390171
2016-01-04   -0.046419
2016-01-05   -0.700592
2016-01-06   -1.414157
Freq: D, Name: b, dtype: float64
"""
# 默认的从0开始 index
df1 = pd.DataFrame(np.arange(12).reshape((3, 4)))
print(df1)

"""
   0  1   2   3
0  0  1   2   3
1  4  5   6   7
2  8  9  10  11
"""
# 手动通过字典构建
df2 = pd.DataFrame({'A': 1.,
                    'B': pd.Timestamp('20130102'),
                    'C': pd.Series(1, index=list(range(4)), dtype='float32'),
                    # 'C': pd.Series(1, index=list(range(1)), dtype='float32'),
                    'D': np.array([3] * 4, dtype='int32'),
                    # 'D': np.array([3], dtype='int32'),
                    'E': pd.Categorical(["test", "train", "test", "train"]),
                    # 'E': pd.Categorical(["test"]),
                    'F': 'foo'})

print(df2)

"""
     A          B    C  D      E    F
0  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
2  1.0 2013-01-02  1.0  3   test  foo
3  1.0 2013-01-02  1.0  3  train  foo
"""
# 查看数据类型
print(df2.dtypes)

"""
df2.dtypes
A           float64
B    datetime64[ns]
C           float32
D             int32
E          category
F            object
dtype: object
"""
# 查看序列
print(df2.index)
# Int64Index([0, 1, 2, 3], dtype='int64')

# 查看列名
print(df2.columns)
# Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
# 查看值
print(df2.values)
"""
array([[1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],
       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo'],
       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'test', 'foo'],
       [1.0, Timestamp('2013-01-02 00:00:00'), 1.0, 3, 'train', 'foo']], dtype=object)
"""
# 数据分布 describe
df2.describe()
"""
         A    C    D
count  4.0  4.0  4.0
mean   1.0  1.0  3.0
std    0.0  0.0  0.0
min    1.0  1.0  3.0
25%    1.0  1.0  3.0
50%    1.0  1.0  3.0
75%    1.0  1.0  3.0
max    1.0  1.0  3.0
"""
# 转置
print(df2.T)
"""                   
0                    1                    2  
A                    1                    1                    1   
B  2013-01-02 00:00:00  2013-01-02 00:00:00  2013-01-02 00:00:00   
C                    1                    1                    1   
D                    3                    3                    3   
E                 test                train                 test   
F                  foo                  foo                  foo   

                     3  
A                    1  
B  2013-01-02 00:00:00  
C                    1  
D                    3  
E                train  
F                  foo  
"""
# 排序 索引、值 axis=0行变化方向(列) axis=1行 ascending由下标变化来定义降序
print(df2.sort_index(axis=1, ascending=False))

"""
     F      E  D    C          B    A
0  foo   test  3  1.0 2013-01-02  1.0
1  foo  train  3  1.0 2013-01-02  1.0
2  foo   test  3  1.0 2013-01-02  1.0
3  foo  train  3  1.0 2013-01-02  1.0
"""

df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=['b', 'a', 'c', 'd'])
'''
                   b         a         c         d
2016-01-01 -1.245780 -0.663392 -0.863734  1.036091
2016-01-02  1.809287 -1.394420 -0.142563  0.865132
2016-01-03 -0.234351 -0.153714 -1.170806  1.120429
2016-01-04 -0.623782 -0.346412  0.465074  0.056842
2016-01-05  0.388427 -0.103269 -0.175382 -0.977384
2016-01-06  0.491857  0.287198  0.716403 -0.811462
'''
df.sort_index(axis=1, ascending=False)
'''
                   d         c         b         a
2016-01-01  1.036091 -0.863734 -1.245780 -0.663392
2016-01-02  0.865132 -0.142563  1.809287 -1.394420
2016-01-03  1.120429 -1.170806 -0.234351 -0.153714
2016-01-04  0.056842  0.465074 -0.623782 -0.346412
2016-01-05 -0.977384 -0.175382  0.388427 -0.103269
2016-01-06 -0.811462  0.716403  0.491857  0.287198
'''

df.sort_index(axis=0, ascending=True)
'''
                   b         a         c         d
2016-01-01 -1.245780 -0.663392 -0.863734  1.036091
2016-01-02  1.809287 -1.394420 -0.142563  0.865132
2016-01-03 -0.234351 -0.153714 -1.170806  1.120429
2016-01-04 -0.623782 -0.346412  0.465074  0.056842
2016-01-05  0.388427 -0.103269 -0.175382 -0.977384
2016-01-06  0.491857  0.287198  0.716403 -0.811462
'''
print(df2.sort_values(by='B'))

"""
     A          B    C  D      E    F
0  1.0 2013-01-02  1.0  3   test  foo
1  1.0 2013-01-02  1.0  3  train  foo
2  1.0 2013-01-02  1.0  3   test  foo
3  1.0 2013-01-02  1.0  3  train  foo
"""